﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace fbasync
{
    [DataContract, Serializable]
    public class Attachment
    {
        [DataMember]
        public string caption { get; set; }
        [DataMember]
        public string description { get; set; }
        [DataMember]
        public string href { get; set; }
        [DataMember]
        public string latitude { get; set; }
        [DataMember]
        public string longitude { get; set; }
        [DataMember]
        public List<AttachmentMedia> media { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public AttachmentProperty properties { get; set; }

        #region JSON serializations
        public string ToJson()
        {
            var attdct = new Dictionary<string, string>();
            if (properties != null)
            {
                var pd = new Dictionary<string, string>(2);

                if (properties.category != null)
                {
                    var cd = new Dictionary<string, string>(2);
                    cd.Add("href", properties.category.href);
                    cd.Add("text", properties.category.text);

                    pd.Add("category", cd.ToJsonAssociativeArray());
                }

                pd.Add("ratings", properties.ratings);

                attdct.Add("properties", pd.ToJsonAssociativeArray());
            }

            if (media != null && media.Count > 0)
            {
                var lst = new List<string>(media.Count);
                foreach (var item in media)
                {
                    var dict = new Dictionary<string, string>(8);
                    dict.Add("type", item.Type.ToString());

                    switch (item.Type)
                    {
                        case AttachmentMediaType.flash:
                            var flash = (AttachmentMediaFlash)item;
                            dict.Add("width", flash.width.ToString());
                            dict.Add("swfsrc", flash.swfsrc);
                            dict.Add("imgsrc", flash.imgsrc);
                            dict.Add("height", flash.height.ToString());
                            dict.Add("expanded_width", flash.expanded_width.ToString());
                            dict.Add("expanded_height", flash.expanded_height.ToString());
                            break;
                        case AttachmentMediaType.image:
                            var img = (AttachmentMediaImage)item;
                            dict.Add("src", img.src);
                            dict.Add("href", img.href);
                            break;
                        case AttachmentMediaType.mp3:
                            var mp3 = (AttachmentMediaMp3)item;
                            dict.Add("title", mp3.title);
                            dict.Add("src", mp3.src);
                            dict.Add("artist", mp3.artist);
                            dict.Add("album", mp3.album);
                            break;
                        case AttachmentMediaType.video:
                            var video = (AttachmentMediaVideo)item;
                            dict.Add("video_title", video.video_title);
                            dict.Add("video_src", video.video_src);
                            dict.Add("video_link", video.video_link);
                            dict.Add("preview_img", video.preview_img);
                            break;
                    }

                    lst.Add(dict.ToJsonAssociativeArray());
                }

                attdct.Add("media", lst.ToJsonArray());
            }

            attdct.Add("name", name);
            attdct.Add("href", href);
            attdct.Add("caption", caption);
            attdct.Add("description", description);
            attdct.Add("latitude", latitude);
            attdct.Add("longitude", longitude);

            return attdct.ToJsonAssociativeArray();
        }
        #endregion
    }

    [DataContract, Serializable]
    public enum AttachmentMediaType
    {
        [EnumMember]
        image,
        [EnumMember]
        flash,
        [EnumMember]
        mp3,
        [EnumMember]
        video,
    }

    [DataContract, Serializable]
    public abstract class AttachmentMedia
    {
        [DataMember]
        public AttachmentMediaType Type { get; internal set; }
    }

    [DataContract, Serializable]
    public class AttachmentMediaImage : AttachmentMedia
    {
        public AttachmentMediaImage()
        {
            Type = AttachmentMediaType.image;
        }
        [DataMember]
        public string href { get; set; }
        [DataMember]
        public string src { get; set; }
    }

    [DataContract, Serializable]
    public class AttachmentMediaFlash : AttachmentMedia
    {
        public AttachmentMediaFlash()
        {
            Type = AttachmentMediaType.flash;
        }

        [DataMember]
        public int expanded_height { get; set; }
        [DataMember]
        public int expanded_width { get; set; }
        [DataMember]
        public int height { get; set; }
        [DataMember]
        public string imgsrc { get; set; }
        [DataMember]
        public string swfsrc { get; set; }
        [DataMember]
        public int width { get; set; }
    }

    [DataContract, Serializable]
    public class AttachmentMediaMp3 : AttachmentMedia
    {
        public AttachmentMediaMp3()
        {
            Type = AttachmentMediaType.mp3;
        }

        [DataMember]
        public string album { get; set; }
        [DataMember]
        public string artist { get; set; }
        [DataMember]
        public string src { get; set; }
        [DataMember]
        public string title { get; set; }
    }

    [DataContract, Serializable]
    public class AttachmentMediaVideo : AttachmentMedia
    {
        public AttachmentMediaVideo()
        {
            Type = AttachmentMediaType.video;
        }

        [DataMember]
        public string preview_img { get; set; }
        [DataMember]
        public string video_link { get; set; }
        [DataMember]
        public string video_src { get; set; }
        [DataMember]
        public string video_title { get; set; }
    }

    [DataContract, Serializable]
    public class AttachmentProperty
    {
        [DataMember]
        public ActionLink category { get; set; }
        [DataMember]
        public string ratings { get; set; }
    }
}
